package com.yz.house.ui.fragment;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Size;
import android.view.View;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.makeramen.roundedimageview.RoundedImageView;
import com.youth.banner.Banner;
import com.youth.banner.loader.ImageLoader;
import com.yz.house.R;
import com.yz.house.base.BaseFragment;
import com.yz.house.func.ListDecoration;
import com.yz.house.model.DecorateBean;
import com.yz.house.ui.activity.ContentDetailActivity;
import com.yz.house.ui.adaper.DecorateListAdapter;
import com.yz.house.ui.utils.ContentUtils;
import com.yz.house.ui.utils.ImageUtils;
import com.yz.house.ui.utils.SizeUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.BindView;

/**
 *
 * @author zhangyf
 * @date 2018/4/12 0012
 */
public class DecorateFragment extends BaseFragment {

    @BindView(R.id.banner)
    Banner banner;
    @BindView(R.id.rv_prepare)
    RecyclerView rvPrepare;
    @BindView(R.id.rv_ing)
    RecyclerView rvIng;
    @BindView(R.id.rv_end)
    RecyclerView rvEnd;
    DecorateListAdapter prepareAdapter;
    DecorateListAdapter ingAdapter;
    DecorateListAdapter endAdapter;
    private List<Integer> imgs = Arrays.asList(R.mipmap.img_banner1,R.mipmap.img_banner2,
            R.mipmap.img_banner3, R.mipmap.img_banner4);
    private List<DecorateBean> prepareList = Arrays.asList(new DecorateBean(R.mipmap.decorate_before1),new DecorateBean(R.mipmap.decorate_before2),
            new DecorateBean(R.mipmap.decorate_before3), new DecorateBean(R.mipmap.decorate_before4),new DecorateBean(R.mipmap.decorate_before5));

    private List<DecorateBean> ingList = Arrays.asList(new DecorateBean(R.mipmap.decorate_ing1),new DecorateBean(R.mipmap.decorate_ing2),
            new DecorateBean(R.mipmap.decorate_ing3), new DecorateBean(R.mipmap.decorate_ing4),new DecorateBean(R.mipmap.decorate_ing5)
    ,new DecorateBean(R.mipmap.decorate_ing6));

    private List<DecorateBean> endList = Arrays.asList(new DecorateBean(R.mipmap.decorate_end1),new DecorateBean(R.mipmap.decorate_end2),
            new DecorateBean(R.mipmap.decorate_end3), new DecorateBean(R.mipmap.decorate_end4),new DecorateBean(R.mipmap.decorate_end5));
    public static DecorateFragment newInstance(Bundle args) {
        DecorateFragment fragment = new DecorateFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected void onInitLazyData() {

    }

    @Override
    protected void onInitFastData() {

    }

    @Override
    protected void onInitFastView() {
        banner.setImageLoader(new GlideImageLoader());
        banner.setImages(imgs);
        banner.setDelayTime(3000);
        banner.start();
        List<DecorateBean> newList = ContentUtils.getDecorateList(1);
        for (int i = 0; i < newList.size(); i++) {
            newList.get(i).setImg(prepareList.get(i).getImg());
        }
        prepareAdapter = new DecorateListAdapter(newList);
        prepareAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Bundle bundle = new Bundle();
                bundle.putString("img",prepareAdapter.getData().get(position).getImg_detail());
                bundle.putString("content",prepareAdapter.getData().get(position).getContent());
                mActivity.readyGo(ContentDetailActivity.class,bundle);
            }
        });
        rvPrepare.setLayoutManager(new LinearLayoutManager(mActivity,LinearLayoutManager.HORIZONTAL,false));
        rvPrepare.addItemDecoration(new ListDecoration(SizeUtils.dp2px(10)));
        rvPrepare.setAdapter(prepareAdapter);

        List<DecorateBean> newList2 = ContentUtils.getDecorateList(2);
        for (int i = 0; i < newList2.size(); i++) {
            newList2.get(i).setImg(ingList.get(i).getImg());
        }
        ingAdapter = new DecorateListAdapter(newList2);
        ingAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Bundle bundle = new Bundle();
                bundle.putString("img",ingAdapter.getData().get(position).getImg_detail());
                bundle.putString("content",ingAdapter.getData().get(position).getContent());
                mActivity.readyGo(ContentDetailActivity.class,bundle);
            }
        });
        rvIng.setLayoutManager(new LinearLayoutManager(mActivity,LinearLayoutManager.HORIZONTAL,false));
        rvIng.addItemDecoration(new ListDecoration(SizeUtils.dp2px(10)));
        rvIng.setAdapter(ingAdapter);

        List<DecorateBean> newList3 = ContentUtils.getDecorateList(3);
        for (int i = 0; i < newList3.size(); i++) {
            newList3.get(i).setImg(endList.get(i).getImg());
        }
        endAdapter = new DecorateListAdapter(newList3);
        endAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Bundle bundle = new Bundle();
                bundle.putString("img",endAdapter.getData().get(position).getImg_detail());
                bundle.putString("content",endAdapter.getData().get(position).getContent());
                mActivity.readyGo(ContentDetailActivity.class,bundle);
            }
        });
        rvEnd.setLayoutManager(new LinearLayoutManager(mActivity,LinearLayoutManager.HORIZONTAL,false));
        rvEnd.addItemDecoration(new ListDecoration(SizeUtils.dp2px(10)));
        rvEnd.setAdapter(endAdapter);
    }

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.fragment_decorate;
    }

    public class GlideImageLoader extends ImageLoader {
        @Override
        public void displayImage(Context context, Object path, ImageView imageView) {
            RoundedImageView imge = (RoundedImageView) imageView;
            imge.setCornerRadius(SizeUtils.dp2px(3));
            imge.setImageResource((Integer) path);
            imge.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mActivity.readyGo(ContentDetailActivity.class);
                }
            });
        }

        @Override
        public ImageView createImageView(Context context) {
            return new RoundedImageView(context);
        }
    }
}
